//package cn.controlsys.common.base;
//
//import cn.controlsys.common.constants.HttpCode;
//import cn.controlsys.common.exception.BaseException;
//import cn.controlsys.common.exception.IllegalParameterException;
//import cn.controlsys.common.support.BaseErrorCode;
//import cn.controlsys.common.util.PropertiesFileUtil;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import org.apache.commons.lang.StringUtils;
//import org.apache.shiro.authz.UnauthorizedException;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.ui.ModelMap;
//import org.springframework.validation.BindingResult;
//import org.springframework.validation.FieldError;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.List;
//
///**
// * Author: shanzheng <br/>
// * Description: 控制器基类<br/>
// * Date： 17:41 2017/8/5.<br/>
// * Modified By: <br/>
// */
//public abstract class BaseController<T> {
//
//    private final static Logger logger = LoggerFactory.getLogger(BaseController.class);
//
//    /** 异常处理 */
//    @ExceptionHandler(Exception.class)
//    public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
//            throws Exception {
//        logger.error("OH,MY GOD! SOME ERRORS OCCURED! AS FOLLOWS",ex);
//        ModelMap modelMap = new ModelMap();
//        if (ex instanceof BaseException) {
//            ((BaseException) ex).handler(modelMap);
//        } else if (ex instanceof IllegalArgumentException) {
//            new IllegalParameterException(ex.getMessage()).handler(modelMap);
//        } else if (ex instanceof UnauthorizedException) {
//            modelMap.put("code", HttpCode.FORBIDDEN.value());
//            modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
//        } else {
//            modelMap.put("code", HttpCode.INTERNAL_SERVER_ERROR.value());
//            String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
//            modelMap.put("msg", msg);// msg.length() > 100 ? "系统走神了,请稍候再试." : msg
//        }
//        response.setContentType("application/json;charset=UTF-8");
//        modelMap.put("timestamp", System.currentTimeMillis());
////        logger.info("RESPONSE : " + JSON.toJSON(modelMap));
//        byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
//        response.getOutputStream().write(bytes);
//
////        logger.error("OH,MY GOD! SOME ERRORS OCCURED! AS FOLLOWS");
////        ModelMap modelMap = new ModelMap();
////        if (ex instanceof BaseException) {
////            ((BaseException) ex).handler(modelMap);
////            logger.info("RESPONSE : " + JSON.toJSON(modelMap));
////        } else if (ex instanceof IllegalArgumentException) {
////            new IllegalParameterException(ex.getMessage()).handler(modelMap);
////            logger.info("RESPONSE : " + JSON.toJSON(modelMap));
////        } else if (ex instanceof UnauthorizedException) {
////            modelMap.put("code", HttpCode.FORBIDDEN.value());
////            modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
////            logger.info("RESPONSE : " + JSON.toJSON(modelMap));
////        } else {
////            modelMap.put("code", HttpCode.INTERNAL_SERVER_ERROR.value());
////            String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
////            modelMap.put("exception",ex.toString().length() > 50 ? "系统走神,异常过长，查看'msg'":ex.getMessage());
////            modelMap.put("msg", msg);// msg.length() > 100 ? "系统走神了,请稍候再试." : msg
////            logger.info("RESPONSE : " + JSON.toJSON(modelMap));
////        }
////        response.setContentType("application/json;charset=UTF-8");
////        modelMap.put("timestamp", System.currentTimeMillis());
////        byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
////        response.getOutputStream().write(bytes);
//    }
//
//
//    /**
//     * Description: 返回jsp视图<br/>
//     * Param: [path：路径 ] <br/>
//     * Author: shanzheng <br/>
//     * Date: 9:37 2017/8/7 <br/>
//     * Return: java.lang.String <br/>
//     */
//    public static String jsp(String path) {
//        return path.concat(".jsp");
//    }
//
//
//
//    //todo 获取当前用户id
//
//
//
//
//    /**
//     * Description: 设置失败响应代码 -- 传递ServiceErrorCode
//     * @param serviceErrorCode 服务层错误枚举类
//     * @author: shanzheng
//     * @date: 15:00 2017/10/24
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
////    protected  ResponseEntity<ModelMap> setFailModelMap(ServiceErrorCode serviceErrorCode) {
////        return setFailModelMap(HttpCode.BAD_REQUEST, serviceErrorCode);
////    }
//
//    /**
//     * Description: 设置失败响应代码 -- 传递HttpCode、ServiceErrorCode
//     * @param httpCode 响应code枚举类
//	 * @param serviceErrorCode 服务层错误枚举类
//     * @author: shanzheng
//     * @date: 14:20 2017/10/24
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
////    protected  ResponseEntity<ModelMap> setFailModelMap(HttpCode httpCode,ServiceErrorCode serviceErrorCode) {
////        return setBaseErrorModelMap(httpCode,serviceErrorCode,null,HttpStatus.OK);
////    }
//
//    /**
//     * Description: 设置成功响应代码 -- 不传参数
//     * @author: shanzheng
//     * @date: 12:02 2017/10/13
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
//    protected ResponseEntity<ModelMap> setSuccessModelMap() {
//        return setSuccessModelMap(null);
//    }
//
//
//    /**
//     * Description: 设置成功响应代码 -- 返回数据data
//	 * @param data 响应code值
//     * @author: shanzheng
//     * @date: 11:18 2017/10/13
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
//    public static ResponseEntity<ModelMap> setSuccessModelMap(Object data) {
//        return setModelMap(HttpCode.OK, data);
//    }
//
//    /**
//     * Description: 设置响应代码
//	 * @param code 响应code枚举类
//	 * @param data 响应的数据
//     * @author: shanzheng
//     * @date: 11:12 2017/10/13
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
//    protected static ResponseEntity<ModelMap> setModelMap(HttpCode code, Object data) {
//        ModelMap modelMap = new ModelMap();
//        modelMap.put("code",code.value());
//        modelMap.put("msg",code.msg());
//        if (data != null) {
//            if (data instanceof List<?>) {
//                modelMap.put("data",data);
//                modelMap.put("iTotalRecords", ((List<?>) data).size());
//                modelMap.put("iTotalDisplayRecords", ((List<?>) data).size());
//            } else {
//                modelMap.put("data",data);
//            }
//        }
//        modelMap.put("timestamp",System.currentTimeMillis());
////        logger.info("RESPONSE : " + JSON.toJSONString(modelMap));
//        return ResponseEntity.ok(modelMap);
//    }
//
//    /**
//
//    /**
//     * Description: 设置错误响应代码 -- 便于外部扩展(例如：业务层错误)
//     * @param code http响应枚举类
//	 * @param subCode (业务层)错误响应代码
//	 * @param subMsg (业务层)错误响应信息
//	 * @param httpStatus http响应状态
//     * @author: shanzheng
//     * @date: 14:02 2017/11/3
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
//    protected ResponseEntity<ModelMap> setBaseErrorModelMap(HttpCode code, Integer subCode, String subMsg, HttpStatus httpStatus) {
//        ModelMap modelMap = new ModelMap();
//        modelMap.put("code",code.value());
//        modelMap.put("msg",code.msg());
//        modelMap.put("sub_code",subCode);//错误类型
//        modelMap.put("sub_msg",subMsg);//错误原因
//        modelMap.put("timestamp",System.currentTimeMillis());
//        logger.info("RESPONSE : " + JSON.toJSONString(modelMap));
//        return new ResponseEntity<ModelMap>(modelMap, httpStatus);
//    }
//
//    /**
//     * Description: 设置错误响应代码 -- 便于外部扩展(例如：业务层错误)
//     * @param subCode (业务层)错误响应代码
//     * @param subMsg (业务层)错误响应信息
//     * @author: shanzheng
//     * @date: 14:02 2017/11/3
//     * @return: org.springframework.http.ResponseEntity<org.springframework.ui.ModelMap>
//     */
//    protected ResponseEntity<ModelMap> setBaseErrorModelMap(Integer subCode, String subMsg) {
//        ModelMap modelMap = new ModelMap();
//        HttpCode code = HttpCode.OK;
//        modelMap.put("code",code.value());
//        modelMap.put("msg",code.msg());
//        modelMap.put("sub_msg",subMsg);//错误原因
//        modelMap.put("sub_code",subCode);//错误类型
//        modelMap.put("timestamp",System.currentTimeMillis());
//        logger.info("RESPONSE : " + JSON.toJSONString(modelMap));
//        return new ResponseEntity<ModelMap>(modelMap, HttpStatus.OK);
//    }
//
//}
